<?php

namespace app\models;

use app\models\constant\Status;
use app\models\product\Product;
use Yii;

/**
 * This is the model class for table "brand".
 *
 * @property int $id
 * @property string $name 名称
 * @property int $image_id 图片ID
 * @property string|null $tag 标签, 逗号分开
 * @property int $sort
 * @property int $status 1 开, 0 关 -99删除
 * @property int $supplier_id
 * @property string $create_time
 * @property string $update_time
 */
class Brand extends \yii\db\ActiveRecord
{
    // use Status;

    /**
     * status list
     * @var int[]
     */
    public static $statusMapping = [
        'ALL'       => -1,
        'OK'        => 1,
        'CLOSE'     => 0,
        'DELETE'    => -99,
    ];

    /**
     * status display list
     * @var int[]
     */
    public static $statusMappingDisplay = [
        '全部'       => -1,
        '正常'       => 1,
        '待审核'       => 0,
        // '删除'       => -99,
    ];

    /**
     * Status list
     *
     * @return array
     */
    public static function statusList()
    {
        $list = [];
        foreach (self::$statusMappingDisplay as $key => $value) {
            $list[] = [
                'label' => $key,
                'value' => $value
            ];
        }

        return $list;
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'brand';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['image_id', 'sort', 'status'], 'integer'],
            [['tag'], 'string'],
            [['create_time', 'update_time'], 'safe'],
            [['name'], 'string', 'max' => 255],

            [['supplier_id'], 'safe']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'image_id' => 'Image ID',
            'tag' => 'Tag',
            'sort' => 'Sort',
            'status' => 'Status',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    /**
     * Save timestamp
     * @param bool $insert
     * @return bool
     */
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            $this->update_time = $time;
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }

    /**
     * clear
     */
    public static function clear()
    {
        $list = self::find()
            ->where(['status' => self::$statusMapping['DELETE']])
            ->all();
        foreach ($list as $brand) {
            $image = Images::findOne(['id' => $brand->image_id]);
            if ($image) {
                $image->delete();
            }
            $brand->delete();
            // echo 'delete image id > ', $image->id, "\n";
            echo 'delete brand id > ', $brand->id, "\n";
        }
    }

    /**
     * with image
     *
     * @return \yii\db\ActiveQuery
     */
    public function getImage()
    {
        return $this->hasOne(Images::class, ['id' => 'image_id']);
    }

    public function getProducts()
    {
        return $this->hasMany(Product::class, ['brand_id' => 'id'])
            ->where(['status' => Product::$statusMapping['OK']]);
    }
}
